using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace NBusiness.Data
{
    /// <summary>
    /// A simple collection of entities.
    /// </summary>
    public class EntityCollection : CollectionBase, IEntityCollection
    {
        public EntityCollection() { }
        public EntityCollection(params EntityBase[] entities)
        {
            InnerList.AddRange(entities);
        }

        #region IEntityCollection Members
        /// <summary>
        /// Gets or sets an entity in this collection at the given index.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public EntityBase this[int index]
        {
            get
            {
                return (EntityBase)InnerList[index];
            }
            set
            {
                InnerList[index] = value;
            }
        }
        /// <summary>
        /// Get the index of this entity in this collection.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int IndexOf(EntityBase entity)
        {
            return InnerList.IndexOf(entity);
        }
        /// <summary>
        /// Removes an entity from this list. Does not delete this entity on a Save.
        /// </summary>
        /// <param name="entity"></param>
        public void Remove(EntityBase entity)
        {
            InnerList.Remove(entity);
        }
        /// <summary>
        /// Add an entity to this collection.
        /// </summary>
        /// <param name="entity"></param>
        public void Add(EntityBase entity)
        {
            InnerList.Add(entity);
        }
        /// <summary>
        /// Calls Save on each entity in this collection. Does not persist removed entities.
        /// </summary>
        public void Save()
        {
            foreach (EntityBase entity in InnerList)
            {
                entity.Save();
            }
        }

        #endregion
    }
}
